package com.itheima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupEditVo;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    CheckGroupMapper checkGroupMapper;

    @Autowired
    CheckItemMapper checkItemMapper;

    @Override
    public void add(CheckGroup checkGroup) {
        //插入检查组基本信息
        checkGroupMapper.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        List<Integer> checkitemIds = checkGroup.getCheckitemIds();
//        setCheckGroupAndCheckItem(checkitemIds, checkGroupId);
        setCheckGroupAndCheckItemBatch(checkitemIds, checkGroupId);

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //告诉mybatis要分页
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CheckGroup> checkGroups = checkGroupMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), checkGroups);
    }

    @Override
    public CheckGroupEditVo find4Edit(Integer id) {
        CheckGroup checkGroup = checkGroupMapper.findById(id);
        List<CheckItem> checkItems = checkItemMapper.findAll();
        List<Integer> checkItemIds = checkGroupMapper.findCheckItemIdsByCheckGroupId(id);
        CheckGroupEditVo vo = new CheckGroupEditVo();
        vo.setCheckGroup(checkGroup);
        vo.setCheckItemIds(checkItemIds);
        vo.setCheckItems(checkItems);
        return vo;
    }

    @Override
    public void edit(CheckGroup checkGroup) {
        Integer checkGroupId = checkGroup.getId();
        //更新检查组的基本信息
        checkGroupMapper.edit(checkGroup);
        //删除原来关系
        checkGroupMapper.deleteRelation(checkGroupId);
        //重新建立关系
        setCheckGroupAndCheckItemBatch(checkGroup.getCheckitemIds(), checkGroupId);

    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }

    public void setCheckGroupAndCheckItem(List<Integer> checkitemIds, Integer checkGroupId) {
        if (CollectionUtil.isNotEmpty(checkitemIds)) {
            for (Integer checkitemId : checkitemIds) {
                Map map = new HashMap<>();
                map.put("checkGroupId", checkGroupId);
                map.put("checkitemId", checkitemId);
                checkGroupMapper.setCheckGroupAndCheckItem(map);
            }
        }
    }

    public void setCheckGroupAndCheckItemBatch(List<Integer> checkitemIds, Integer checkGroupId) {
        if (CollectionUtil.isNotEmpty(checkitemIds)) {
            List<Map> params = new ArrayList<>();
            for (Integer checkitemId : checkitemIds) {
                Map map = new HashMap<>();
                map.put("checkGroupId", checkGroupId);
                map.put("checkitemId", checkitemId);
                params.add(map);
            }
            checkGroupMapper.setCheckGroupAndCheckItemBatch(params);
        }
    }
}
